package controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import manager.LeaveTypeManager;
import data.dao.LeaveDAOException;
import data.dao.daoImplementation.NotFoundException;
import data.dto.LeaveTypeDTO;
import data.dto.UserDTO;

/**
 * Servlet implementation class AdminLeavetypeServlet
 */
@WebServlet("/AdminLeavetypeServlet")
public class AdminLeavetypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AdminLeavetypeServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// check user
		String page = "/admin/ViewPage.jsp?type=leavetype";
		UserDTO user = util.CurrentUser.getCurrentUser(request.getSession());

		// check user
		if (user == null) {
			page = util.Constants.homePage;
		} else if (!util.CurrentUser.isAdmin(user)) {
			page = util.Constants.homePage;
		} else {

			String type = request.getParameter("type") == null ? "" : request
					.getParameter("type").trim();

			LeaveTypeManager ltm = new LeaveTypeManager();
			ArrayList<LeaveTypeDTO> data = null;

			if (type.equalsIgnoreCase("delete")) {
				// do delete process here
				String leavetype = request.getParameter("id");
				// String strid=request.getParameter("id");
				Integer BI = new Integer(leavetype);
				int id = BI.intValue();
				LeaveTypeDTO lt = new LeaveTypeDTO();
				lt.setId(id);

				try {
					try {
						ltm.deleteLeaveDetailType(lt);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (NotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (data.dao.daoImplementation.SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} catch (LeaveDAOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else if (type.equalsIgnoreCase("insert")) {
				// do insert process here
				LeaveTypeDTO lt = new LeaveTypeDTO();
				lt.setName(request.getParameter("name"));
				lt.setStatus((short) (1));
				try {
					ltm.insertLeaveDetailType(lt);
				} catch (LeaveDAOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (data.dao.daoImplementation.SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else if (type.equalsIgnoreCase("update")) {
				// do update process here
				LeaveTypeDTO lt = new LeaveTypeDTO();
				lt.setName(request.getParameter("name"));
				lt.setStatus(util.Constants.STATUS_SHOW);
				lt.setId(Short.parseShort(request.getParameter("id")));
				try {
					ltm.updateLeaveDetailType(lt);
				} catch (LeaveDAOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (data.dao.daoImplementation.SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}

			try {
				data = ltm.findAllLeaveType();
			} catch (LeaveDAOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (data.dao.daoImplementation.SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			request.setAttribute("leaveType", data);
		}

		RequestDispatcher rd = request.getRequestDispatcher(page);
		rd.forward(request, response);
	}

}
